<?php
//	Примеры:
//	$fs=new filesystem();
//	$fs->chmod_r("/home/marhipov/test_mv",0777,1);
//	$fs->rm_r("/home/marhipov/test_mv");
//	$fs->mkdir_r("/home/marhipov/test_mv/1/5/5",0744);
//	$files=$fs->ls_r("/home/marhipov/test_mv");

class filesystem{
// *******************************************************************************
function mkdir_r($path,$mode=""){
	if(preg_match("/\.\./",$path)){
		echo"<br>\nWarning: mkdir_r('$path') failed. '..' is not allowed. <br>\n";
		return;
	}
	preg_match_all("|[^/]+|",$path,$dirs,PREG_SET_ORDER);
	if($path[0]=="/"){
		$create_path="/";
	}
	foreach($dirs as $dir){
		$create_path.=$dir[0];
		if(!file_exists($create_path)){
			if($mode==""){
				@mkdir($create_path);
			}else{
				@mkdir($create_path,$mode);
			}
		}
		$create_path.="/";
	}
}
// *******************************************************************************
function chmod_r($path,$mode,$do_not_recurse=0,$without_root=0){
	$this->init($path,$do_not_recurse,$with_rootout);
	foreach($this->files as $file){
		@chmod($file["name"],$mode);
	}
	return count($this->files);
}
// *******************************************************************************
function rm_r($path,$do_not_recurse=0,$without_root=0){
	$this->init($path,$do_not_recurse,$without_root);
	$this->files=array_reverse($this->files);
	foreach($this->files as $file){
		if($file["is_dir"]){
			if(!$do_not_recurse)@rmdir($file["name"]);
		}else{
			@unlink($file["name"]);
		}
	}
	return count($this->files);
}
// *******************************************************************************
function ls_r($path,$do_not_recurse=0,$without_root=0){
	$this->init($path,$do_not_recurse,$without_root);
	return $this->files;
}
// *******************************************************************************
function init($path,$do_not_recurse,$without_root){
	$this->files=array();
	$path=preg_replace("|/$|","",$path);
	$root_stat=$this->get_stat($path);
	if(!$without_root){
		$this->files[]=$root_stat;
	}
	if($root_stat["is_dir"]){
		$this->recursive_engine($path,$do_not_recurse);
	}
}
// *******************************************************************************
function recursive_engine($path,$do_not_recurse){
	$dir_id=opendir($path);
	while(($file=readdir($dir_id))!=false){
		if($file!="." && $file!=".."){
			$pathfile=$path."/".$file;
			$stat=$this->get_stat($pathfile);
			$this->files[]=$stat;
			if($stat["is_dir"] && !$do_not_recurse){
				$this->recursive_engine($pathfile,$do_not_recurse);
			}
		}
	}
}
// *******************************************************************************
function get_stat($pathfile){
	$perms=base_convert(@fileperms($pathfile),10,8);
	$stat=array(
		"name"=>$pathfile,
		"perms"=>substr($perms,strlen($perms)-3,3),
		"is_dir"=>is_dir($pathfile),
	);
	return $stat;
}
// *******************************************************************************
}

?>